package pm.gui;

import pm.devices.drives.Drive;
import pm.config.Configuration;
import pm.gui.frames.PMFrame;
import pm.gui.frames.AdminFrame;
import pm.gui.components.basepanels.SplashPanel;
import pm.gui.components.basepanels.MainPanel;
import pm.gui.components.basepanels.PrintPanel;
import pm.gui.components.basepanels.PrintFileChoosePanel;
import pm.core.Session;
import pm.core.PrintProperties;

import javax.swing.*;
import java.io.File;

public class AbstractGuiModule implements GuiModule {

	protected GuiModuleListener guiModuleListener;
	protected Configuration configuration;

	//Frames
	protected PMFrame pmFrame;
	protected AdminFrame adminFrame;
	//Base Panels:
	protected SplashPanel splashPanel;
	protected MainPanel mainPanel;
	protected PrintPanel printPanel;
	protected PrintFileChoosePanel printFileChoosePanel;


	/* GuiModule implementation */

	public void showSplashScreen() {
		pmFrame.setBasePanel(splashPanel);
	}

	public void showMainScreen() {
		pmFrame.setBasePanel(mainPanel);
	}

	public void showFileChooseDialog() {
		showPrintFileChoose();
	}

	public void showError(final String message) {
		//todo
	}

	public Long getHWND() {
		final JPanel panel = pmFrame.getCurrentPanel();
		pmFrame.setBasePanel(printPanel);
		long hwnd = printPanel.getNativeOOViewComponent().getHWND().longValue();
		pmFrame.setBasePanel(panel);
		return hwnd;
	}

	public void updateSessionState(final Session session) {
		//todo
	}

	public void setDrives(final Drive[] drives) {
		//todo refactor
//		this.printFileChoosePanel.setDevices(drives);
	}


	/**
	 * BackButtons from all panels proccessing
	 *
	 * @param panelType source panel
	 */
	protected void backButton(final PanelType panelType) {
		switch (panelType) {
			case Print:
				guiModuleListener.closeCurrentDocument();
				showMainScreen();
				break;
			default: {
				showMainScreen();
			}
		}
	}

	/* "Main" screen controls events proccessing */

	protected void mainPanelScanButton() {
		//todo
	}

	protected void mainPanelPrintButton() {
		showPrintFileChoose();
	}

	protected void mainPanelCopyButton() {
		//todo
	}


	/* "Print" screen controls events proccessing */

	protected void printPanelPrintButton() {
		//todo get print props
		final PrintProperties printProperties = new PrintProperties(1);
		guiModuleListener.print(printProperties);
	}

	protected void documentViewPanelNextButton() {
		guiModuleListener.nextPage();
	}

	protected void documentViewPanelPreviousButton() {
		guiModuleListener.previousPage();
	}


	/* PrintFileChoosePanel events proccessing */

	protected void driveSelected() {
		if (printFileChoosePanel.driveComboBox.getSelectedItem() == null) {
			return;
		}
		Drive drive = (Drive) printFileChoosePanel.driveComboBox.getSelectedItem();
		if (new File(drive.getId() + ":\\").listFiles() == null) {
			this.printFileChoosePanel.fileChooseScrollPane.setVisible(false);
			//todo
			this.printFileChoosePanel.driveNotSelectedLabel.setVisible(true);
			return;
		}

		this.printFileChoosePanel.driveNotSelectedLabel.setVisible(false);
		this.printFileChoosePanel.fileChooseScrollPane.setVisible(true);
		this.printFileChoosePanel.changeDir(drive.getId() + ":\\");
	}

	protected void openFile() {
		Object value = this.printFileChoosePanel.filesList.getSelectedValue();
		if (value instanceof File) {
			File file = (File) value;
			guiModuleListener.openFile(file);
		}
	}

	private void showPrintFileChoose() {
		//todo
		printFileChoosePanel.driveComboBox.setSelectedIndex(-1);
		printFileChoosePanel.textRadioButton.setSelected(true);
		pmFrame.setBasePanel(printFileChoosePanel);
	}
}
